package cn.itcast.bos.web.action.fixedarea;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.fixedarea.FixedAreaService;
import cn.itcast.bos.web.action.base.BaseAction;
import cn.itcast.crm.domain.Customer;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;
	
	
	//解除与快递员关联的方法fixedid:123
	private String nocourierId;
	private String  fixedid;
	
	public void setNocourierId(String nocourierId) {
		this.nocourierId = nocourierId;
	}

	public void setFixedid(String fixedid) {
		this.fixedid = fixedid;
	}

	@Action(value="deleteCourierToFixedArea",results={@Result(name="success",location="/pages/base/fixed_area.html",type="redirect")})
	public String deleteCourierToFixedArea(){
		fixedAreaService.deleteCourierToFixedArea(fixedid,nocourierId);
		
		return SUCCESS;
	}
	
	
	
	//定区关联快递员的方法
	
	private Integer courierId;
	private Integer takeTimeId;
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}
	
	@Action(value="fixedArea_associationCourierToFixedArea",
			results={@Result(name="success",location="/pages/base/fixed_area.html",type="redirect")})
	public String linkedCourier(){
		fixedAreaService.linkedCourier(model,courierId,takeTimeId);
		
		return SUCCESS;
	}
	
	
	
	// 查询已经关联定区的客户
	private String fixedAreaId;

	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}

	@Action(value = "fixedArea_hasLinkedCustomer", results = { @Result(name = "success", type = "json") })
	public String findLinkedCustomers() {
		// 使用webClient 调用webService接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:8090/crm_management/services/customerService/findLinkedCustomers/"
						+ fixedAreaId).accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);

		return SUCCESS;
	}

	// 查询未关联定区的客户
	@Action(value = "fixedArea_noLinkedCustomer", results = { @Result(name = "success", type = "json") })
	public String findNoLinkedCustomers() {
		// 使用webClient 调用webService接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:8090/crm_management/"
						+ "services/customerService/findNoLinkedCustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);

		return SUCCESS;
	}

	// 关联客户到定区的功能
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value = "decidedzone_assigncustomerstodecidedzone", results = { @Result(name = "success", type = "redirect", location = "/pages/base/fixed_area.html") })
	public String linkedCustomerToFixedArea() {
		String customerIdstr = StringUtils.join(customerIds, "-");
		WebClient
				.create("http://localhost:8090/crm_management/services"
						+ "/customerService/linkedCustomerToFixedArea?customerIdStr="
						+ customerIdstr + "&fixedAreaId=" + model.getId()).put(
						null);

		return SUCCESS;
	}

	// 删除功能
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "fixedArea_delete")
	public void delete() {
		fixedAreaService.delete(ids);

	}

	// 分页查询的功能
	@Action(value = "fixedArea_search", results = { @Result(type = "json") })
	public String pageQuery() {
		Pageable pageable = new PageRequest(page - 1, rows);

		Specification<FixedArea> specification = new Specification<FixedArea>() {

			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class),
							model.getId());
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.equal(root.get("company")
							.as(String.class), model.getCompany());
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page page = fixedAreaService.findPageData(specification, pageable);
		pushPageDataToValueStack(page);
		return "success";
	}

	// 保存的功能
	@Action(value = "fixedArea_save", results = { @Result(name = "success", location = "/pages/base/fixed_area.html", type = "redirect") })
	public String save() {
		fixedAreaService.save(model);
		return "success";
	}
}
